﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace DECODER
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int dataLength = 0;
        OpenFileDialog ofd = null;
        FileInfo File = null;
        byte[] file = null;
        int mode = 0;

        FolderBrowserDialog dialog = null;
        DirectoryInfo Folder = null;

        private void WritetoBinFile(string binPath, byte[] buffer, int startIndex, int length)
        {
            FileStream fsWrite = new FileStream(binPath, FileMode.Create, FileAccess.Write);//如果已存在相同文件名的文件,则删掉之前的，创建新的文件！！！
            fsWrite.Write(buffer, startIndex, length);
            fsWrite.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (mode == 2)
            { 
                dataLength = 0;
                ofd = null;
                File = null;
                file = null;
                dialog = null;
                Folder = null;
            }
            ofd = new OpenFileDialog();
            mode = 1;
            ofd.RestoreDirectory = true;
            ofd.Multiselect = false;
            ofd.Title = "请选择文件";
            ofd.InitialDirectory = ".\\";
            ofd.Filter = "文件|*.*";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = ofd.FileName;
                File = new FileInfo(ofd.FileName);
                file = System.IO.File.ReadAllBytes(ofd.FileName);
            }
            else
            {
                return;
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (mode == 1)
            {
                dataLength = 0;
                ofd = null;
                File = null;
                file = null;
                dialog = null;
                Folder=null;
            }
            dialog = new FolderBrowserDialog();
            dialog.Description = "选择一个目录";
            mode = 2;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = dialog.SelectedPath;
                Folder = new DirectoryInfo(dialog.SelectedPath);
            }
        }

        private void FolderchangeName(string path)
        {
            string[] files = Directory.GetFiles(path);
            foreach (string file in files)
            {
                    byte[] bytes = System.IO.File.ReadAllBytes(file);
                System.IO.File.Delete(file);
                WritetoBinFile(file + textBox2.Text.ToString(),bytes,0,bytes.Length);
                    

            }

            string[] subdirectories = Directory.GetDirectories(path);
            foreach (string subdirectory in subdirectories)
            {
                FolderchangeName(subdirectory);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (mode == 1)
            {
                WritetoBinFile(textBox1.Text.ToString() + textBox2.Text.ToString(), file, 0, file.Length);
                textBox1.Text = "完成！";
            }
            else if (mode == 2)
            {
                FolderchangeName(textBox1.Text.ToString());
            }
            else
            {
                textBox2.Text = "请选择文件";
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            checkBox2.Checked = false;
            checkBox3.Checked = false;
            checkBox4.Checked = false;
            textBox2.Enabled = false;
            textBox2.Text = "";
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            checkBox1.Checked = false;
            checkBox3.Checked = false;
            checkBox4.Checked = false;
            textBox2.Enabled = false;
            textBox2.Text = ".1415926535897932384262";
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            checkBox1.Checked = false;
            checkBox2.Checked = false;
            checkBox4.Checked = false;
            textBox2.Enabled = false;
            textBox2.Text = ".71828182845904523536028";
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            checkBox1.Checked = false;
            checkBox2.Checked = false;
            checkBox3.Checked = false;
            textBox2.Enabled = true;
            textBox2.Text = "";
        }


    }
}
